package com.sam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.sam.member;

import com.mysql.jdbc.Driver;

public class member {
	String userid;
	String nickname;
	String pw1;
	String email;
	String useridmsg;
	String nicknamemsg;
	String pw1msg;
	String emailmsg;
	ArrayList<member> allmembers;
	
	public boolean validate(){
		boolean valid = true;
		
		if (userid.length() < 4 || userid.length() > 20) {
			valid = false;
			useridmsg = "帳號長度不符合!!";
		}
		
		if (nickname.length() < 4 || nickname.length() > 20) {
			valid = false;
			nicknamemsg = "暱稱長度不符合!!";
		}
		
		if (email.indexOf("@") == -1){
			valid = false;
			emailmsg = "電子郵件帳號長度不符合!!";
		}
		
		return valid;
	}

	public member() {
		
	}

	public member(String userid, String nickname, String pw1, String email) {
		super();
		this.userid = userid;
		this.nickname = nickname;
		this.pw1 = pw1;
		this.email = email;
	}

	
	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getUseridmsg() {
		return useridmsg;
	}

	public void setUseridmsg(String useridmsg) {
		this.useridmsg = useridmsg;
	}

	public String getNicknamemsg() {
		return nicknamemsg;
	}

	public void setNicknamemsg(String nicknamemsg) {
		this.nicknamemsg = nicknamemsg;
	}

	public String getPw1msg() {
		return pw1msg;
	}

	public void setPw1msg(String pw1msg) {
		this.pw1msg = pw1msg;
	}

	public String getEmailmsg() {
		return emailmsg;
	}

	public void setEmailmsg(String emailmsg) {
		this.emailmsg = emailmsg;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	
	public void setAllmember(ArrayList<member> allmembers) {
		this.allmembers = allmembers;
	}

	public ArrayList<member> getAllmembers(){
        if (allmembers==null)
                allmembers = new ArrayList<member>();
        try {
                DriverManager.registerDriver(new Driver());
                Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102?useUnicode=true&characterEncoding=UTF-8",
                                "jstu", "abc123");
                String sql = "select id,nickname,password,email from users";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()){
                        String userid = rs.getString("id");
                        String nickname = rs.getString("nickname");
                        String password = rs.getString("password");
                        String email = rs.getString("email");
                        member p = new member(userid, nickname, password, email);
                        allmembers.add(p);
                }
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        	}
        return allmembers;
		}
	
	
}
